package org.qf.web;

import org.qf.pojo.Goods;
import org.qf.service.impl.SmbmsGoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @author zzzyyystart
 * @create 2022-05-12-1:53
 */
@WebServlet(value = "/updateGoodsServlet")
public class SmbmsUpdateGoodsServlet extends HttpServlet {
    SmbmsGoodsServiceImpl goodsService = new SmbmsGoodsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String classifyName = (String)req.getSession().getAttribute("classifyName");
        String goodsCode = req.getParameter("goodsCode");
        String name = req.getParameter("name");
        String price = req.getParameter("price");
        String sales = req.getParameter("sales");
        String stock = req.getParameter("stock");
        String imgPath = req.getParameter("imgPath");
        Goods goods =
                new Goods(Integer.valueOf(goodsCode),name,new BigDecimal(price),
                        Integer.valueOf(sales),Integer.valueOf(stock),imgPath);

        int i = goodsService.updateGoods(classifyName, goods);
        if(i>0){
           resp.sendRedirect("goodsServlet?classify="+classifyName);
        }
        else{
            req.getRequestDispatcher("goodsUpdate.jsp").forward(req,resp);
        }

    }
}
